from pydantic import BaseModel, ConfigDict, Field
from typing import Optional, List
from datetime import datetime
from app.admin.schemas.role import Role


class UserBase(BaseModel):
    id: str = Field(default=None, min_length=3, max_length=32, description="用户ID")
    email: Optional[str] = Field(default=None, max_length=50, description="用户邮箱")
    login_name: str = Field(default=None, min_length=1, max_length=50, description="登录名")
    full_name: Optional[str] = Field(default=None, max_length=100, description="用户全名")
    tenant_id: str = Field(default=None, min_length=1, max_length=32, description="租户ID")
    create_at: datetime = Field(default_factory=datetime.now, description="创建时间")
    update_at: datetime = Field(default_factory=datetime.now, description="更新时间")
    is_active: bool = Field(default=True, description="是否激活")
    is_superuser: bool = Field(default=False, description="是否超级用户")


class UserCreate(UserBase):
    password: str


class UserUpdate(UserBase):
    password: Optional[str] = None
    is_active: Optional[bool] = None
    is_superuser: Optional[bool] = None
    full_name: Optional[str] = Field(None, max_length=100, description="用户全名")


class UserInDBBase(UserBase):
    id: str = Field(..., min_length=1, max_length=32, description="用户ID")
    email: Optional[str] = Field(None, max_length=50, description="用户邮箱")
    is_active: bool = Field(..., description="是否激活")
    is_superuser: bool = Field(..., description="是否超级用户")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    roles: List[Role] = Field(..., description="角色列表")

    model_config = ConfigDict(from_attributes=True)


class User(UserInDBBase):
    pass


class UserInDB(UserInDBBase):
    hashed_password: str = Field(..., min_length=1, max_length=128, description="密码哈希")


class AdminResetPassword(BaseModel):
    """管理员重置用户密码"""
    user_id: str = Field(..., min_length=1, max_length=32, description="用户ID")


class ChangePassword(BaseModel):
    """用户修改密码（需要旧密码）"""
    old_password: str = Field(..., min_length=1, description="旧密码")
    new_password: str = Field(..., min_length=6, max_length=50, description="新密码")
